package com.example.demo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: ListTool
 * @Description: 集合Handler
 * @Author: adobe
 * @Email software8888@163.com
 * @Version: v1.0
 */
public class ListTool {
    /**
     * @param lists 容错的目标集合的集合
     * @param faultTolerance  容错 a-b-x...=abx...
     * @return 结果集
     * @throws Exception  容错不能大于等于集合数量!
     */
    public static List<String> getIdentical(List<List<String>> lists,
                                            String faultTolerance) throws Exception {
        List<String> res=new ArrayList<>();
        List<String> allList=new ArrayList<>();
        List<String> finalAllList = allList;
        lists.forEach(e-> finalAllList.addAll(e));
        allList=finalAllList.stream().distinct().collect(Collectors.toList());
        List<Integer> conditions=new ArrayList<>();
        int listsLen=lists.size();
        try {
            Arrays.stream(faultTolerance.split("-")).forEach(e -> conditions.add(Integer.parseInt(e)));
        }catch (Exception e){
            throw  new Exception("faultTolerance中只能包含数字!");
        }
        if(conditions.stream().filter(e-> e.intValue()>=listsLen).count()>0)
            throw new Exception("容错不能大于等于集合数量!");
        res =allList.stream().filter(e->conditions.stream().
                filter(e1->(listsLen-e1)==getContainNum(lists,e)).count()>0).collect(Collectors.toList());
        return res;
    }

    public static long getContainNum(List<List<String>> lists,String target){
        long num=0;
        List<List<String>> res;
        num=lists.stream().filter(e->e.contains(target)).count();
        return num;
    }
}